define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                   data:{},
                   popup:0,
                   choose_property_data:{},
                   choose_property_data_copy:{},
                   choose_spec:{},
                   disabled:1,
                   shopInfo:{},
                   shop_id:0,
                  spec_keys_str:''
                };
            },
            el: '#pageGoods',
            created: function() {
                this.fetchData();
            },
              mounted: function() {
                    
                    if (this.mounted == 1) {
                        return;
                    }
                    
                    var that = this ;
                    app.swiper("swiper-container-520275");
                    this.mounted = 1;

            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    this.id=this.$route.params.id;
                     this.loadGoodInfo(); 
                },
                chooseSpec:function(){
                  // this.popup=1;
                  console.log(123);
                  app.show_popup($("#popup_goods"));
                  // this.setData({ popup: 1 })
                },
                btn_cancel:function(){
                   // this.popup=0;
                  // this.setData({ popup:0});
                   app.hide_popup($("#popup_goods"));
                },
                //选规格
                chooseProperty:function(e){
                  // console.log(e); 
                  var property = $(e.target).attr("data-property");
                  var subProperty = $(e.target).attr("data-sub-property");
                  this.choose_property_data_copy={};
                  this.choose_property_data[property] = subProperty;

                  var arr=[];
                  for (var i in this.choose_property_data){
                    arr.push(this.choose_property_data[i]);
                  }
                  // console.log(this.data.property_arr.length);

                  if (arr.length == this.data.property_arr.length){
                    this.spec_keys_str=arr.join(" ");
                    // this.setData({ spec_keys_str: arr.join(" ") });
                    var l = arr.length;
                    for (var j = 0; j < (5-l);j++ ){
                      arr.push("");
                    }
                    // console.log(arr,arr);
                    var spec_keys = arr.sort();
                    var spec_keys_str = spec_keys.join("_");
                    if (this.data.goods_spec[spec_keys_str] && this.data.goods_spec[spec_keys_str].spec_stock>0) {
                      this.choose_spec = this.data.goods_spec[spec_keys_str] ;
                      this.disabled=0;
                    }else{
                      app.alert('已售空');
                      this.disabled=1;
                      // this.setData({ disabled:1});
                    }
                  }
                  this.choose_property_data_copy = this.choose_property_data;
                  // console.log(this.choose_property_data_copy);
                  
                  // console.log(this.choose_spec);
                  //choose_spec
                },
                //加载购物车
                loadGoodInfo(reload = 0) {
                  var that = this;
                  if (reload == 1) {
                    this.loading = 0;
                    this.data = {};
                  }
                  this.loading = 1;
                  app.get("mall/goods/find", {
                    id: this.id
                  }, function (res) {
                    that.loading = 0;
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    that.data = res.data;
                    that.shop_id = res.data.shop_id;
                    that.getShopInfo();
                    app.setTitle(res.data.goods_name);
                    if (that.data.property_arr.length>0){
                      that.disabled=1;
                    }else{
                       that.disabled=0;
                    }
                  });
                },
                 gotCart: function () {
                   
                  this.$router.push({name:'cart',params:{shop_id:this.shop_id}});
                },

                getShopInfo:function(){
                  var that = this;
                  app.get("/mall/shop/find", {id:this.shop_id}, function (res) {
                    
                    if (res.code != 200) {
                      app.alert(res.msg);
                    } else {
                       
                      that.shopInfo = res.data;

                    }
                  });
                },

                buyNow: function (e){
                  // console.log(e);
                  var postData = {};
                  var type = $(e.target).attr("data-type");
                  if (this.data.property_arr.length > 0 && this.choose_spec.spec_stock>0 ){
                    postData.goods_id = this.choose_spec.goods_id;
                    postData.goods_spec_id = this.choose_spec.id;
                    //可以去结算
                  } else if (this.data.property_arr.length===0){
                    postData.goods_id = this.data.id;
                  }else{
                    this.chooseSpec();
                    return ;
                  }
                  //加入购车
                  this.loading = 1;
                  var that = this;
                  this.loading=1;
                  this.disabled=1;
                  app.get("mall/cart/add", postData, function (res) {
                    that.loading = 0;
                    that.loading=0;
                    that.disabled=0;
                     
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    // console.log();
                    if (type =="buyNow"){
                      that.$router.push({ name: 'checkout', params: { shop_id: res.data.shop_id } });
                    }else{
                      that.btn_cancel();
                      app.confirm({
                        text:'成功加入购物车',
                        confirmText:'去结算',
                        cancelText:'返回',
                        yes:function(){

                           that.$router.push({ name: 'checkout', params: { shop_id: res.data.shop_id } });
                        },
                        no:function(){
                            
                            that.$router.go(-1);
                        }
                      });

                    
                      
                    }
                    
                  });
                },

                 
 
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
